home *** CD-ROM | disk | FTP | other *** search
- Path: news.mel.aone.net.au!OzEmail!usenet
- From: tedjones@voyager.co.nz (Ted Jones)
- Newsgroups: comp.lang.c++
- Subject: Problem with polymorphism.
- Date: Thu, 04 Jan 1996 14:10:43 GMT
- Organization: Ted Jones Motor Co Ltd.
- Message-ID: <4cgjkr$fcr@oznet07.ozemail.com.au>
- NNTP-Posting-Host: ts1p08.net.hamilton.voyager.co.nz
- X-Newsreader: Forte Free Agent 1.0.82
-
- Yo All! (Email reply's please I don't read regularly enough)
-
- I have a problem with polymorphisim. :(
-
- Here's some code to chew on.
-
- #ifndef _KEY_HPP_
- #define _KEY_HPP_
-
- class TKey {
- private:
- // In derived classes there should be a "key1" variable defined
- here.
- public:
- // TKey's destructer is virtual as there may be several different
- types of keys
- // with different allocation methods and hence different
- de-allocation methods.
- virtual ~TKey(void) {};
- // Returns 0 if keys are equal, 1 if key2 is greater and -1 if key2
- is l
- virtual int Compare(TKey *key2) = 0;
- };
-
- At this level TKey is abstract and knows nothing of it's descendents.
- Here is it's decendent.
-
- class iKey: public TKey {
- private:
- int key;
- public:
- iKey(int i) { key = i; }
- // virtual ~iTKey(void) {};
- virtual int Compare(TKey *key2);
- virtual int GetKey(void) { return key; }
- };
-
- And here is it's implemented pure virtual function Compare();
-
- int iKey::Compare(TKey *key2)
- {
- if(key == key2->GetKey()){
- return 0;
- } else {
- if(key < key2->GetKey()){
- return 1;
- } else {
- return -1;
- }
- }
- }
-
- This doesn't work tho because Compare is passed a TKey param and TKey
- doesn't have the GetKey() member. I don't think it should because it
- doesn't have data to "get". And if I do declare it there I run into
- other problems. Now if you understand what I'm trying to do and know
- how to do it properly please share it with me.
-
- Basically Compare is called by some other class. Passing in a pointer
- to another object derived from the TKey class. Through polymorphisim
- it should call the right GetKey(). Admittedly it would only be sane
- to pass in a pointer to a iKey becuase only iKeys should be compared
- with iKeys. But I don't know what would happen if I was to declare
- the original pure virtual function as:
-
- virtual int Compare(void) = 0;
-
- and then in all derived classes declare it as:
-
- virtual int Compare(iKey *key2);
-
- etc. Can I overload virtual functions like that safely? But then I
- also run into the problem with the other class calling
- TKey->Compare(tkey); Because the other class only knows about TKey
- and if I decalare it with (void) the other class wont compile. I am
- basically stuck as to what I can do. PLEASE HELP ME. Sob sob. :_( I'm
- loving C++ so far. It's just when I do weird shit (which is just
- plain not possible in C) I get stuck quite badly.
-
- BTW here's the "other class" I was talking about.
-
- class TBTree { // An incomplete btree class. Yeah
- // yeah I know there are lots of class
- private: // libs out there that do this for me.
- typedef struct btree_node { // But I'm learning and do it all the
- TKey *key; // hard way to be a better programmer.
- TStorable *data;
- btree_node *left, *right;
- } TBTree_node;
-
- TBTree_node *btree_root;
- TBTree_node *current;
-
- int size;
- bool success;
-
- void RecurseInsert(TBTree_node *root, TBTree_node *node);
- public:
- TBTree(void) { btree_root = NULL; size = 0; success = TRUE;}
- virtual ~TBTree(void);
-
- virtual void Insert(TKey *key, TStorable *data);
-
- int Size(void) { return size; }
- bool Success(void) { return success; }
- };
-
- And here's where it calls Compare();
-
- void TBTree::RecurseInsert(TBTree_node *root, TBTree_node *node)
- {
- if (root == NULL){
- root = node;
- } else {
- switch((root->key->Compare(node->key))){
- case 0:
- success = TRUE;
- root = node; // Overwrite existing
- nodes.
- break;
- case 1:
- RecurseInsert(root->right, node); // Insert on right.
- break;
- case -1:
- RecurseInsert(root->left, node); // Insert on left.
- break;
- default:
- // Somehthing is amiss. Abnormal Termination.
- assert(((root->key->Compare(node->key)) != 0) &&
- ((root->key->Compare(node->key)) != 1) &&
- ((root->key->Compare(node->key)) != -1)
- );
- break;
- }
- }
- }
-
- - Dreamer/Reality
- --- timEd-B10
- * Origin: æ The Digital Dream's lonely point. (3:774/1500.1)
-
- Ted Jones (Ted Jones Motor Co Ltd.) | Aki Enterprise (Manager) Ph: (045)625-0877
- PO Box 74, Taypo, New Zealand | 20-27 Honmokuhara, Office: (045)621-3031
- Phone: (07)-378-7494 | Naka-ku, Yokohama 231 Hand: (030)27-99191
- Fax: (07)-378-7478 | Japan Fax: (045)622-1420
- Cellular: (025)-960-399 | After Hours Fax: (045)253-3239
- ...-... 'Exporters of Quality Used Japanese Assembled Vehicles' ...-...
-
-